/**
 *  Copyright 2010 Kyle O'Connor, Justin Arling, Mike Mercieca
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package edu.drexel.sd0910.ece01.aqmon.gui;

import java.awt.BorderLayout;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;

import org.apache.log4j.Logger;

import edu.drexel.sd0910.ece01.aqmon.manager.Manager;

/**
 * This class implements the full-screen exclusive API - http://tinyurl.com/y9wjunj
 * 
 * @author Kyle O'Connor
 * 
 */
@SuppressWarnings("serial")
public class FullScreenView extends JFrame implements Observer {

	private final static Logger log = Logger.getLogger(FullScreenView.class);
	
	//gui
	private GraphicsDevice defaultDevice;
	private DisplayMode origDisplayMode;
	
	private MainModel model; //data model

	/**
	 * @throws HeadlessException
	 */
	public FullScreenView(MainModel aModel) throws HeadlessException {
		this.model = aModel;
		setup();
	}

	/**
	 * @param title
	 * @throws HeadlessException
	 */
	public FullScreenView(MainModel aModel, String title) throws HeadlessException {
		super(title);
		this.model = aModel;
		setup();
	}

	private void setup() {
		// setup this full screen frame
		setTitle("AQMon");

		//get graphics environment
		defaultDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		origDisplayMode = defaultDevice.getDisplayMode();
		
		// shutdown procedure
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent w) {
				log.debug("System shutting down...");
				try {
					List<Manager> managers = model.getActiveManagers();
					for (Manager m : managers) {
						m.shutdown();
						log.debug(m + " shutdown");
					}
				} catch (Exception e) {
					log.error("Error occurred during shutdown");
					e.printStackTrace();
				} finally {
					defaultDevice.setDisplayMode(origDisplayMode);
					System.exit(0);
				}
			}
		});

		setLayout(new BorderLayout(0, 10)); // set default layout

		if (defaultDevice.isFullScreenSupported()) {
			// Enter full-screen mode witn an undecorated,
			// non-resizable JFrame object.
			setUndecorated(true);
			setResizable(false);
			// Make it happen!
			defaultDevice.setFullScreenWindow(this);
			validate();
		} else {
			log.error("Full-screen mode not supported");
		}
		
		this.pack();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	public void update(Observable o, Object arg) {

	}

}
